package com.renwei.yunlong.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import java.math.BigDecimal;

import io.github.xudaojie.qrcodelib.zxing.DensityUtils;

/**
 * Created by wangqi on 2017/11/26.
 * 报表 指针数据
 */

public class WorkLineView extends View {
    private float number = 0f;
    private float allNumber = 0f; //百分比
    private DIRECTION direction = DIRECTION.LEFT;
    private LOACTION loaction = LOACTION.TOP;
    private float y = 1;
    private Paint mPaint;

    public enum DIRECTION {
        RIGHT,
        LEFT
    }

    public enum LOACTION {
        TOP,
        BOTTOM
    }

    public void setData(float number, float allNumber, DIRECTION direction,LOACTION loaction) {
        if (number==0){
            setVisibility(GONE);
            return;
        }
        setVisibility(VISIBLE);
        this.number = number;
        this.allNumber = allNumber;
        this.direction = direction;
        this.loaction = loaction;
        invalidate();
    }

    public WorkLineView(Context context) {
        this(context, null);
    }

    public WorkLineView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WorkLineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float[] pts;
        float x;
        if (direction == DIRECTION.LEFT) {
            x = 0;
            mPaint.setTextAlign(Paint.Align.LEFT);
            mPaint.setColor(Color.parseColor("#689DF8"));
            if (loaction == LOACTION.TOP) {
                pts = new float[]{
                        0, DensityUtils.dp2px(getContext(),17),  DensityUtils.dp2px(getContext(),50), DensityUtils.dp2px(getContext(),17),
                        DensityUtils.dp2px(getContext(),50), DensityUtils.dp2px(getContext(),17),  DensityUtils.dp2px(getContext(),61), DensityUtils.dp2px(getContext(),28)
                };
            }else{
                pts = new float[]{
                        0, DensityUtils.dp2px(getContext(),17), DensityUtils.dp2px(getContext(),50), DensityUtils.dp2px(getContext(),17),
                        DensityUtils.dp2px(getContext(),50), DensityUtils.dp2px(getContext(),17),  DensityUtils.dp2px(getContext(),61), DensityUtils.dp2px(getContext(),5)
                };
            }
        } else {
            x = DensityUtils.dp2px(getContext(),61);
            mPaint.setTextAlign(Paint.Align.RIGHT);
            mPaint.setColor(Color.parseColor("#E15656"));
            if (loaction == LOACTION.TOP) {
                pts = new float[]{
                        0, DensityUtils.dp2px(getContext(),28), DensityUtils.dp2px(getContext(),12), DensityUtils.dp2px(getContext(),17),
                        DensityUtils.dp2px(getContext(),12), DensityUtils.dp2px(getContext(),17),  DensityUtils.dp2px(getContext(),61), DensityUtils.dp2px(getContext(),17)
                };
            }else{
                pts = new float[]{
                        0, DensityUtils.dp2px(getContext(),5), DensityUtils.dp2px(getContext(),12), DensityUtils.dp2px(getContext(),17),
                        DensityUtils.dp2px(getContext(),12), DensityUtils.dp2px(getContext(),17), DensityUtils.dp2px(getContext(),61), DensityUtils.dp2px(getContext(),17)
                };
            }
        }
        canvas.drawLines(pts, mPaint);

        mPaint.setColor(Color.parseColor("#828A99"));
        mPaint.setTextSize(DensityUtils.dp2px(getContext(),13));
        // 百分比
        canvas.drawText(new BigDecimal(number*100/ allNumber).setScale(0,BigDecimal.ROUND_HALF_UP) +"%", x,  DensityUtils.dp2px(getContext(),12), mPaint);
        // 数量
        canvas.drawText((int)number+"单", x,  DensityUtils.dp2px(getContext(),32), mPaint);
    }
}
